package com.example.deal.broker.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.example.deal.broker.entity.Deal;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class BrokerController {
	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${server.port}")
	private String port; // 注入普通字符串
	
	@GetMapping("/deal/{id}")
	@HystrixCommand(fallbackMethod = "findByIdFallBack")
	public Deal findById(@PathVariable Long id) {
		return this.restTemplate.getForObject("http://microservice-deal-cloud/deal/" + id, Deal.class);
	}
	
	/**
	 * findById fallBack方法
	 * 主方法与fallback方法应保持一致(入参、返回值)
	 * @param id
	 * @return
	 */
	public Deal findByIdFallBack(Long id){
		Deal deal = new Deal();
		deal.setId(id.intValue());
		deal.setName("Dustyone");
		deal.setPort(port);
		return deal;
  }
}